package com.baiyue.aop;


import com.alibaba.fastjson.JSON;
import com.baiyue.annotation.SystemLog;
import com.baiyue.entity.Log;
import com.baiyue.entity.UserEntity;
import com.baiyue.exception.GlobalException;
import com.baiyue.result.CodeMsg;
import com.baiyue.service.LogService;
import com.baiyue.service.UserService;
import com.baiyue.utils.IPUtils;
import com.baiyue.utils.ObjectUtil;
import com.baiyue.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Spring AOP实现日志管理
 *
 * @author WuBo
 */
@Aspect
@Component
@Slf4j
public class SystemLogAspect {

    private static final ThreadLocal<Date> beginTimeThreadLocal = new NamedThreadLocal<Date>("ThreadLocal beginTime");


    @Autowired
    private LogService logService;

    @Autowired
    private UserService userService;

    @Autowired(required = false)
    private HttpServletRequest request;

    /**
     * Controller层切点,注解方式
     */
    @Pointcut("@annotation(com.baiyue.annotation.SystemLog)")
    public void controllerAspect() {

    }

    /**
     * 前置通知 (在方法执行之前返回)用于拦截Controller层记录用户的操作的开始时间
     *
     * @param joinPoint 切点
     * @throws InterruptedException
     */
    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) throws InterruptedException {
        //线程绑定变量（该数据只有当前请求的线程可见）
        try {
            Integer type = (Integer) getControllerMethodInfo(joinPoint).get("type");
            if (!Objects.equals(type,1) && !Objects.equals(type, 6)) {
                //此处只用于判断用户是否登录，如果未登录，则抛出异常
                userService.getLoginUser(request);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new GlobalException(CodeMsg.USER_NOT_LOGIN);
        }
        Date beginTime = new Date();
        beginTimeThreadLocal.set(beginTime);
    }


    /**
     * 后置通知(在方法执行之后并返回数据) 用于拦截Controller层无异常的操作
     *
     * @param joinPoint 切点
     */
    @AfterReturning("controllerAspect()")
    public void after(JoinPoint joinPoint) {
        try {
            String username = "";
            String description = getControllerMethodInfo(joinPoint).get("description").toString();
            Map<String, String[]> requestParams = request.getParameterMap();

            Log log = new Log();

            Integer type = (Integer) getControllerMethodInfo(joinPoint).get("type");
            //请求用户
            if (Objects.equals(type, 6)){
                username = "push";
            }
            //后台操作(非登录)
            if (!Objects.equals(type,1) && !Objects.equals(type, 6)) {
                //后台操作请求(已登录)
                UserEntity user = userService.getLoginUser(request);
                if (user != null) {
                    username = user.getUsername();
                }
            }
            log.setUsername(username);
            //日志标题
            log.setName(description);
            //日志类型
            log.setLogType(type);
            //日志请求url
            log.setRequestUrl(request.getRequestURI());
            //请求方式
            log.setRequestType(request.getMethod());
            //请求参数
            //如果请求参数 为字符串类型 则上报正常
            if (!Objects.equals(ObjectUtil.mapToString(requestParams), "{}")){
                log.setRequestParam(ObjectUtil.mapToString(requestParams));
                //如果请求参数为实体类类型 则获取json后存入
            }else if (joinPoint.getArgs() != null){
                Object[] get = joinPoint.getArgs();
                for (Object obj : get){
                    log.setRequestParam(JSON.toJSONString(obj));
                }
            }else {
                log.setRequestParam("");
            }


            //其他属性
            log.setIp(IPUtils.getIpAddr(request));
            if (!"".equals(username) && username != null){
                log.setCreateBy(username);
                log.setUpdateBy(username);
            }else {
                log.setCreateBy(request.getParameter("username"));
                log.setUpdateBy(request.getParameter("username"));
            }
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setDelFlag(0);

            //.......
            //请求开始时间
            long beginTime = beginTimeThreadLocal.get().getTime();
            long endTime = System.currentTimeMillis();
            //请求耗时
            Long logElapsedTime = endTime - beginTime;
            log.setCostTime(logElapsedTime.intValue());

            //持久化(存储到数据或者ES，可以考虑用线程池)
            //logService.insert(log);
            ThreadPoolUtil.getPool().execute(new SaveSystemLogThread(log, logService));

        } catch (Exception e) {
            log.error(e.getMessage());
            new GlobalException(CodeMsg.USER_NOT_LOGIN);
        }
    }


    /**
     * 保存日志至数据库
     */
    private static class SaveSystemLogThread implements Runnable {

        private Log log;
        private LogService logService;

        public SaveSystemLogThread(Log esLog, LogService logService) {
            this.log = esLog;
            this.logService = logService;
        }

        @Override
        public void run() {
            logService.insert(log);
        }
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param joinPoint 切点
     * @return 方法描述
     * @throws Exception
     */
    public static Map<String, Object> getControllerMethodInfo(JoinPoint joinPoint) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>(16);
        //获取目标类名
        String targetName = joinPoint.getTarget().getClass().getName();
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        //获取相关参数
        Object[] arguments = joinPoint.getArgs();
        //生成类对象
        Class targetClass = Class.forName(targetName);
        //获取该类中的方法
        Method[] methods = targetClass.getMethods();

        String description = "";
        Integer type = null;

        for (Method method : methods) {
            if (!method.getName().equals(methodName)) {
                continue;
            }
            Class[] clazzs = method.getParameterTypes();
            if (clazzs.length != arguments.length) {
                //比较方法中参数个数与从切点中获取的参数个数是否相同
                continue;
            }
            description = method.getAnnotation(SystemLog.class).description();
            type = method.getAnnotation(SystemLog.class).type().ordinal();
            map.put("description", description);
            map.put("type", type);
        }
        return map;
    }

}
